﻿using System;
using System.Collections.Generic;
using System.Xml;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Model;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class StaticDataQuerier : IStaticDataQuerier
    {     
        private const string ProcessConfigName = "Process.xml";
        private readonly XmlDocument _document;

        public lmbcprintlogModel GetlmbcprintlogModel(string barCode)
        {
            var list = GetlmbcprintlogModels(String.Format(" FBarCode='{0}' ", barCode));
            if (list.Count == 0)
                return null;
            return list[0];
        }

        public StaticDataQuerier()
        {
            _document = new XmlDocument();
        }

        public List<Persist.BLL.Model.lmbcprintlogModel> GetlmbcprintlogModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new lmbcprintlogDao();
            return dao.GetList(sqlWhere);
        }

        public List<Persist.BLL.Model.tEmpModel> GetEmpModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new tEmpDao();
            return dao.GetList(sqlWhere);
        }

        public List<Persist.BLL.Model.VIcItemQueryModel> GetVIcItemQueryModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = condition;
            var dao = new VIcItemQueryDao();
            return dao.GetList(sqlWhere);
        }

        public VIcItemQueryModel GetVIcItemQueryModel(int itemId)
        {
            var list = GetVIcItemQueryModels(String.Format(" FItemID = {0} ", itemId));
            if (list.Count == 0)
                return null;
            return list[0];
        }

        public VIcItemQueryModel GetVIcItemQueryModel(string number)
        {
            var list = GetVIcItemQueryModels(String.Format(" FNumber = '{0}' ", number));
            if (list.Count == 0)
                return null;
            return list[0];
        }

        public List<Persist.BLL.Model.tOrganizationModel> GetOrganizationModels(string condition)
        {
            //var sqlWhere = new SqlItem();
            //sqlWhere.Text = condition;
            //var dao = new tOrganizationDao();
            //return dao.GetList(sqlWhere);
            return null;
        }

        public List<Persist.BLL.Model.tSupplierModel> GetSupplierModes(string condition)
        {
            //var sqlWhere = new SqlItem();
            //sqlWhere.Text = condition;
            //var dao = new tSupplierDao();
            //return dao.GetList(sqlWhere);
            return null;
        }
        
        public List<Model.Process> GetProcessList()
        {
            _document.Load(System.Environment.CurrentDirectory + "\\" + ProcessConfigName);
            var childNodes = _document.DocumentElement.ChildNodes;
            var list = new List<Process>();
            foreach (var childNode in childNodes)
            {
                var process = new Process();
                process.ProcessNo = ((XmlElement) childNode).Attributes["name"].Value;
                process.ProcessName = ((XmlElement)childNode).Attributes["value"].Value;
                list.Add(process);
            }
            return list;
        }


        public List<TProcedureModel> GetProcedureModelList()
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = " procedureno not in(select parentnumber from TProcedure where parentnumber is not null and parentnumber <> '') ";
            var dao = new TProcedureDao();
            return dao.GetList(sqlWhere);
        }
    }
}
